package com.huibur.devicecheck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huibur.devicecheck.enums.DeviceEnums;
import com.huibur.devicecheck.mapper.AssignTaskMapper;
import com.huibur.devicecheck.mapper.DeviceMapper;
import com.huibur.devicecheck.model.param.AssignTaskParam;
import com.huibur.devicecheck.model.param.JudgePowerParam;
import com.huibur.devicecheck.model.po.AssignTaskPO;
import com.huibur.devicecheck.model.po.DevicePO;
import com.huibur.devicecheck.request.ObjRes;
import com.huibur.devicecheck.request.SessionContext;
import com.huibur.devicecheck.service.IAssignTaskService;
import com.huibur.devicecheck.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/*******************************************************
* @title: AssignTaskServiceImpl.java
* @description:
* @author wushiqi
* @date 2021/9/29
* @version 1.0
*******************************************************/
@Service
public class AssignTaskServiceImpl extends ServiceImpl<AssignTaskMapper,AssignTaskPO> implements IAssignTaskService {

    @Autowired
    AssignTaskMapper assignTaskMapper;
    @Autowired
    IAssignTaskService iAssignTaskService;
    @Autowired
    DeviceMapper deviceMapper;

    /**
     * 分配管理
     * @param assignTaskParam
     * @return
     */
    @Override
    public ObjRes<Void> assignTasks(AssignTaskParam assignTaskParam) {
        List<AssignTaskPO> assignTaskPOList = new ArrayList<>();

        List<Long> collect = assignTaskParam.getDeviceIdList().stream().collect(Collectors.toList());
        for (Long aLong : collect) {

            AssignTaskPO assignTask = assignTaskMapper.selectOne(new LambdaQueryWrapper<AssignTaskPO>()
                    .eq(AssignTaskPO::getDeviceId, aLong)
                    .ne(AssignTaskPO::getUserId,assignTaskParam.getUserId())
                    .eq(AssignTaskPO::getIsDeleted,0));
            if (null != assignTask) {
                assignTask.setIsDeleted(1);
                assignTaskMapper.updateById(assignTask);
            }
            AssignTaskPO assignTaskPO = new AssignTaskPO();
            assignTaskPO.setUserId(assignTaskParam.getUserId());
            assignTaskPO.setDeviceId(aLong);
            assignTaskPO.setIsDeleted(0);
            assignTaskPO.setCreateAt(DateUtils.getCurrentTimestamp());
            assignTaskPO.setCreateBy(SessionContext.getCurrentUserSubject().getUserId());
            assignTaskPOList.add(assignTaskPO);
        }
        iAssignTaskService.saveBatch(assignTaskPOList);
        return new ObjRes<Void>().success("分配成功");
    }

    /**
     * 判断是否有提交权限
     * @param judgePowerParam
     * @return
     */
    @Override
    public Boolean judgePower(JudgePowerParam judgePowerParam) {
        String label = judgePowerParam.getLabel();
        Long userId = judgePowerParam.getUserId();

        DevicePO devicePO = deviceMapper.selectOne(new LambdaQueryWrapper<DevicePO>()
                .eq(DevicePO::getLabel, label)
                .eq(DevicePO::getIsDeleted, DeviceEnums.NORMAL.getCode()));

        AssignTaskPO assignTaskPO = assignTaskMapper.selectOne(new LambdaQueryWrapper<AssignTaskPO>()
                .eq(AssignTaskPO::getUserId, userId)
                .eq(AssignTaskPO::getDeviceId, devicePO.getId())
                .eq(AssignTaskPO::getIsDeleted, 0));
        if (null == assignTaskPO) {
            return false;
        }
        return true;
    }
}
